import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';

import { createHtmlPlugin } from 'vite-plugin-html';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    console.log(`${process.cwd()}/src/project/proA/index.html`);
    const getViteEnv = (mode: any, target: any) => {
        return loadEnv(mode, `${process.cwd()}/.env`)[target];
    };
    return {
        envDir: '/.env',
        plugins: [
            vue(),
            createHtmlPlugin({
                entry: '/src/project/proA/main.ts',
                // template: '/src/project/proA/index.html',
                inject: {
                    data: {
                        title: getViteEnv(mode, 'VITE_SYSTEM_TITLE')
                    }
                }
            }),
            AutoImport({
                imports: [
                    'vue',
                    {
                        '@proA/API': [['default', 'request']]
                    }
                ],
                eslintrc: {
                    enabled: false, // Default `false`,需要更新文件的时候把他打开，更新完之后再关闭，否则每次都要生成文件会有问题
                    filepath: 'src/project/proA/.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
                    globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
                },
                dts: 'src/project/proA/auto-imports.d.ts'
            }),
            Components({
                dts: 'src/project/proA/components.d.ts',
                dirs: ['src/project/proA/views'],
                directoryAsNamespace: true //允许子目录作为组件的命名空间前缀。
            })
        ],
        resolve: {
            alias: {
                '@proA': fileURLToPath(new URL('./', import.meta.url)),
                '@': `${process.cwd()}/src`
            }
        },
        build: {
            outDir: 'src/project/proA/dist',
            rollupOptions: {
                // input: 'src/project/proA/main.ts',
                output: {
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    entryFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
                }
            }
        }
    };
});
